package library.persistence.impl;

import library.domain.User;
import library.persistence.DBUtil;
import library.persistence.dao.UserDao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    private static final String INSERT_USER =
            "INSERT INTO userinfo (username,password,studentid,ismanager) VALUES(?,?,?,?)";

    private  static final String GET_USER_BY_ID = "SELECT * FROM userinfo WHERE id = ?";

    private  static final String GET_USER_BY_USERNAME = "SELECT * FROM userinfo WHERE username = ?";

    private  static final String GET_ALL_USER = "SELECT * FROM userinfo";

    private  static final String GET_USER_BY_USERNAME_AND_PASSWORD = "SELECT * FROM userinfo WHERE username = ? AND password = ?";


    @Override
    public List<User> getAllUser() {
        List<User> userList = new ArrayList<>();

        Connection connection = DBUtil.getConnection();
        try{
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(GET_ALL_USER);
            while(resultSet.next()){
                int id = resultSet.getInt(1);
                String username = resultSet.getString("username");
                String password = resultSet.getString(3);
                String studentId = resultSet.getString(4);
                int isManager = resultSet.getInt(5);
                userList.add(new User(id,username,password,studentId,isManager));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closeStatement(statement);
            DBUtil.closeConnection(connection);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return userList;
    }

    @Override
    public User getUserByUsernameAndPassword(String username, String password) {
        User user = new User();
        Connection connection = DBUtil.getConnection();
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_BY_USERNAME_AND_PASSWORD);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2,user.getPassword());
            preparedStatement.executeUpdate();
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User getUserByID(int id) {
        User user = new User();
        Connection connection = DBUtil.getConnection();
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_BY_ID);
            preparedStatement.setInt(1,id);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                user.setStudentId(resultSet.getString(4));
                user.setIsManager(resultSet.getInt(5));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public User getUserByUsername(String username) {
        User user = new User();
        Connection connection = DBUtil.getConnection();
        try{
            PreparedStatement preparedStatement = connection.prepareStatement(GET_USER_BY_USERNAME);
            preparedStatement.setString(1,username);
            ResultSet resultSet = preparedStatement.executeQuery();
            if(resultSet.next()){
                user.setId(resultSet.getInt(1));
                user.setUsername(resultSet.getString(2));
                user.setPassword(resultSet.getString(3));
                user.setStudentId(resultSet.getString(4));
                user.setIsManager(resultSet.getInt(5));
            }
            DBUtil.closeResultSet(resultSet);
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        }catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public void updateUserByID(User user) {
        System.out.println("方法暂时未定义");
    }

    @Override
    public void deleteUserByID(int id) {
        System.out.println("方法暂时未定义");
    }

    @Override
    public void deleteUserByUsername(String username) {
        System.out.println("方法暂时未定义");
    }

    @Override
    public boolean insertUser(User user) {
        boolean result = false;
        Connection connection = DBUtil.getConnection();
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(INSERT_USER);
            preparedStatement.setString(1,user.getUsername());
            preparedStatement.setString(2, user.getPassword());
            preparedStatement.setString(3,user.getStudentId());
            preparedStatement.setInt(4,user.getIsManager());
            int rows = preparedStatement.executeUpdate();
            if(rows == 1){
                result = true;
            }
            DBUtil.closePreparedStatement(preparedStatement);
            DBUtil.closeConnection(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<User> searchUserList(String keyword) {
        System.out.println("方法暂时未定义");
        return null;
    }
}
